package com.mine.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mine.pojo.MineLine;
import com.mine.pojo.MineLineOrder;
import com.mine.result.CodeMsg;
import com.mine.result.Result;
import com.mine.service.LineService;

@Controller
public class LineController {

	@Autowired
	LineService lineService;
	
	//排队者获取队列信息
	@RequestMapping(value="/line/{shopId}/{token}")
	@ResponseBody
	public Result getShopLines(@PathVariable Long shopId,@PathVariable String token){
		if (shopId==null||token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = lineService.getShopLines(shopId,token);
		return result;
	}
	//排队者获取某一队列详情信息 如果该人已经加入队列 那么把队列信息返回
	@RequestMapping(value="/line/detail/{lineId}/{token}")
	@ResponseBody
	public Result getLineDetail(@PathVariable Long lineId,@PathVariable String token){
		if (lineId==null||token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = lineService.getLineDetail(lineId,token);
		return result;
	}
	
	//排队者加入到队列
	@RequestMapping(value="/line/join/{token}")
	@ResponseBody
	public Result addShopLines(@RequestBody MineLineOrder lineOrder,@PathVariable String token){
		if (lineOrder==null||lineOrder.getShopId()==null||token==null||lineOrder.getLineId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = lineService.addShopLine(lineOrder,token);
		return result;
	}
	
	//排队者取消队列
	@RequestMapping(value="/line/cancel/{token}")
	@ResponseBody
	public Result updateCancelShopLines(@RequestBody MineLineOrder lineOrder,@PathVariable String token){
		//System.out.println(lineOrder);
		if (lineOrder==null||token==null||lineOrder.getId()==null||lineOrder.getLineId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = lineService.updateCancelShopLines(lineOrder,token);
		return result;
	}
	/*******  管理者操作  *********/
	//操作者创建队列
	@RequestMapping(value="/line/create/manager/{token}")
	@ResponseBody
	public Result managerAddLines(@RequestBody MineLine line,@PathVariable String token){
		if (line==null||line.getLineName()==null||line.getShopId()==null||line.getPerPretime()==null||line.getMaxActive()==null||line.getMaxWait()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = lineService.addNewShopLine(line,token);
		return result;
	}
	
	//操作者关闭队列
	@RequestMapping(value="/line/close/manager/{token}")
	@ResponseBody
	public Result managerCloseShopLines(@RequestBody MineLine line,@PathVariable String token){
		if (line==null||line.getId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		line.setStatus(3);//关闭排队 状态 1.正常 2.暂停排队 3.关闭
		Result result = lineService.updateShopLine(line,token);
		return result;
	}
	
	//操作者暂停队列
	@RequestMapping(value="/line/pause/manager/{token}")
	@ResponseBody
	public Result managerShopLines(@RequestBody MineLine line,@PathVariable String token){
		if (line==null||line.getId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		line.setStatus(2);//关闭排队 状态 1.正常 2.暂停排队 3.关闭
		Result result = lineService.updateShopLine(line,token);
		return result;
	}
	
	//操作者叫下一位 如果当前号码 line.getCurrentNumber()有值 说明当前轮到号没到 
	@RequestMapping(value="/line/next/manager/{token}")
	@ResponseBody
	public Result managerShopLinesNext(@RequestBody MineLine line,@PathVariable String token){
		if (line==null||line.getId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		//line.getCurrentNumber();
		Result result = lineService.updateShopLineNext(line,token);
		return result;
	}
}
